package blobQuickstart.blobAzureApp.interceptor;

import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.util.StringUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.dubbo.common.json.JSONObject;
import com.dearbinge.data.redis.service.RedisService;
//import com.google.common.cache.Cache;
import com.dearbinge.utils.ResultMsg;
import com.dearbinge.utils.enums.ResultStatusCodeEnum;

public class Interceptor implements HandlerInterceptor {

//    private static final Logger log = LoggerFactory.getLogger(LoginInterceptor.class);


//	 @Autowired
//	 private RedisService redisService;

    /**
     * 进入controller层之前拦截请求
     * @param httpServletRequest
     * @param httpServletResponse
     * @param o
     * @return
     * @throws Exception
     */
    @Override
    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o ) throws Exception {

//        System.out.println("---------------------开始进入请求地址拦截----------------------------");
//        try {
//            ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
//            String sessionId = RequestContextHolder.getRequestAttributes().getSessionId();
//            HttpServletRequest request = attributes.getRequest();
//            String key = sessionId + "-" + request.getServletPath();
//            if (StringUtils.isEmpty(redisService.get(key))) {// 如果缓存中有这个url视为重复提交
//	            redisService.setEx(key,1,key);
//	        } else {
//	        	System.out.println("重复提交=="+key);
//	        	//重置response
//	        	httpServletResponse.reset();
//	        	//设置编码格式
//	        	httpServletResponse.setCharacterEncoding("UTF-8");
//	        	httpServletResponse.setContentType("application/json;charset=UTF-8");
//	        	PrintWriter pw = httpServletResponse.getWriter();
//	        	String string1 = "{ \"statusCode\": 500,\"data\": null, \"msg\": \"系统异常\"}";
//	        	pw.write(string1);
//	        	return false;
//	        }
//        } catch (Throwable e) {
//            e.printStackTrace();
//            System.out.println("验证重复提交时出现未知异常!");
//        }
        return true;

    }

    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {
//        System.out.println("--------------处理请求完成后视图渲染之前的处理操作---------------");
    }

    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {
//        System.out.println("---------------视图渲染之后的操作-------------------------0");
    }



}